﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Threading;
using System.Xml;
using Sector7.Framework.Consts;
using Sector7.Framework.Core;
using Sector7.Framework.Utility;

namespace Sector7.Framework.Windows
{
    /// <summary>
    /// SplashWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SplashWindow : Window
    {
        #region Accessor
        /// <summary>
        /// 加载成功事件
        /// </summary>
        public event EventHandler LoadSuccess;

        /// <summary>
        /// 加载失败事件
        /// </summary>
        public event EventHandler LoadFailure;
        #endregion

        #region Public functions
        #endregion

        #region Private functions
        private void LoadProductInformation()
        {
            lblProgressMessage.Content = "加载产品信息...";

            m_systemConfigManager = new ConfigurationManager(
                string.Format(@"{0}\{1}", C_DirectoryName.CONFIG, C_FileName.SYSTEM_CONFIG));

            Senvironments.ProductConfig = m_systemConfigManager.GetProductConfiguration();

            lblProductName.Content = Senvironments.ProductConfig.ProductName;
            lblCodeName.Content = Senvironments.ProductConfig.CodeName;
            lblVersion.Content = Senvironments.ProductConfig.FullVersion;
        }

        private void LoadConfiguration()
        {
            LoadSystemConfiguration();

            LoadUserConfiguration();
            Thread.Sleep(INTERVAL);

            InitializeMainWindowByConfiguration();
            while (!m_isLoadConfigurationCompleted)
            {
                Thread.Sleep(INTERVAL);
            }

            LoadCredentials();
            Thread.Sleep(INTERVAL);

            VerifyCredentials();
            Thread.Sleep(INTERVAL);

            InitializeMainWindowByCredentials();
            Thread.Sleep(INTERVAL);
        }

        private void LoadSystemConfiguration()
        {
            this.Dispatcher.BeginInvoke(new UpdateProgressDelegate(UpdateProgress), "加载系统配置...");
            Thread.Sleep(INTERVAL);

            this.Dispatcher.BeginInvoke(new UpdateProgressDelegate(UpdateProgress), "加载系统模块配置...");
            Senvironments.ModuleConfigList = m_systemConfigManager.GetModuleConfiguration();
            Thread.Sleep(INTERVAL);

            this.Dispatcher.BeginInvoke(new UpdateProgressDelegate(UpdateProgress), "加载系统导航配置...");
            Senvironments.NavigationConfigList = m_systemConfigManager.GetNavigationConfiguration();
            Thread.Sleep(INTERVAL);
        }

        private void LoadUserConfiguration()
        {
            this.Dispatcher.BeginInvoke(new UpdateProgressDelegate(UpdateProgress), "加载用户配置...");

            //
        }

        private void InitializeMainWindowByConfiguration()
        {
            this.Dispatcher.BeginInvoke(new UpdateProgressDelegate(UpdateProgress), "根据配置初始化主窗口...");

            Senvironments.MainWindow.LoadConfigurationCompleted += new EventHandler(MainWindow_LoadConfigurationCompleted);

            this.Dispatcher.BeginInvoke(new UpdateMainWindowDelegate(delegate()
            {
                Senvironments.MainWindow.LoadConfiguration();
            }));
        }

        private void InitializeMainWindowByCredentials()
        {
            this.Dispatcher.BeginInvoke(new UpdateProgressDelegate(UpdateProgress), "根据数字证书初始化主窗口...");
        }

        private void MainWindow_LoadConfigurationCompleted(object sender, EventArgs e)
        {
            m_isLoadConfigurationCompleted = true;
        }

        /// <summary>
        /// 加载数字证书
        /// </summary>
        private void LoadCredentials()
        {
            this.Dispatcher.BeginInvoke(new UpdateProgressDelegate(UpdateProgress), "加载数字证书...");

            //

            this.Dispatcher.BeginInvoke(new UpdateMainWindowDelegate(delegate()
            {
                Senvironments.MainWindow.LoadCredentials();
            }));
        }

        /// <summary>
        /// 验证数字证书
        /// </summary>
        private void VerifyCredentials()
        {
            this.Dispatcher.BeginInvoke(new UpdateProgressDelegate(UpdateProgress), "向远程服务器验证数字证书合法性...");

            //
            //MessageBox.Show("该数字证书已过期，请联系admin@sector7.cc更新。");
            //OnLoadFailure(new EventArgs());
        }

        /// <summary>
        /// 更新进度信息
        /// </summary>
        /// <param name="message">进度信息</param>
        private void UpdateProgress(string message)
        {
            lblProgressMessage.Content = message;
        }

        /// <summary>
        /// 触发加载成功事件
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnLoadSuccess(EventArgs e)
        {
            if (LoadSuccess != null)
            {
                LoadSuccess(this, e);
            }
        }

        /// <summary>
        /// 触发加载失败事件
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnLoadFailure(EventArgs e)
        {
            if (LoadFailure != null)
            {
                LoadFailure(this, e);
            }
        }

        private void Window_Initialized(object sender, EventArgs e)
        {
            LoadProductInformation();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            m_worker = BackgroundWorkerUtil.GetWorker(this);
            m_worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
            m_worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);

            m_worker.RunWorkerAsync();
        }

        private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            OnLoadSuccess(new EventArgs());
        }

        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            LoadConfiguration();
        }
        #endregion

        #region Structure
        /// <summary>
        /// 构造启动窗口对象
        /// </summary>
        public SplashWindow()
        {
            InitializeComponent();
        }
        #endregion

        #region Inner members
        private BackgroundWorker m_worker = null;
        private ConfigurationManager m_systemConfigManager = null;

        private bool m_isLoadConfigurationCompleted = false;

        private const int INTERVAL = 100;

        private delegate void UpdateProgressDelegate(string message);
        private delegate void UpdateMainWindowDelegate();
        #endregion
    }
}
